﻿using ClusteringProblem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClusteringProblem.Maths.Metrics
{
    /// <summary>
    /// Miara cosinusowa
    /// </summary>
    public class CosineDistance : IDistance
    {
        public double ComputeDistance(IVectorType x, IVectorType y)
        {
            int vectorLength = x.Vector.Length;
            double top = 0, left = 0, right = 0;

            for (int i = 0; i < vectorLength; ++i)
            {
                top += x.Vector[i] * y.Vector[i];
                left += Math.Pow(x.Vector[i], 2.0);
                right += Math.Pow(y.Vector[i], 2.0);
            }

            return 1.0 - (top / (Math.Sqrt(left) * Math.Sqrt(right)));
        }
    }
}
